package com;

import java.util.ArrayList;
import java.util.Scanner;

public class TechnicianTest {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        ArrayList<Technician> list = new ArrayList<>();

        while (true){
            System.out.println("-----------欢迎来到召唤师峡谷-----------");
            System.out.println("1、添加英雄");
            System.out.println("2、修改英雄");
            System.out.println("3、删除英雄");
            System.out.println("4、查看英雄");
            System.out.println("5、退出");
            System.out.println("请输入您的选择：");

            String choice = sc.next();

            switch (choice){
                case "1":
                    addHero(list);
                    break;
                case "2":
                    replaceHero(list);
                    break;
                case "3":
                    deleteHero(list);
                    break;
                case "4":
                    searchHero(list);
                    break;
                case "5":
                    System.out.println("感谢您的使用，溜溜球！");
                    return;
                default:
                    System.out.println("您的输入有误，哈麻皮！");
                    break;
            }
        }
    }

    private static void searchHero(ArrayList<Technician> list) {
        if (list.size() == 0) {
            System.out.println("暂无英雄信息，请添加之后再查询！");
            return;
        }
        System.out.println("编号\t英雄名\t\t年龄\t位置");
        for (int i = 0; i < list.size(); i++) {
            Technician te = list.get(i);
            System.out.println(te.getId() + "\t" + te.getName() + "\t" + te.getAge() + "\t\t" + te.getAddress());
        }

    }

    private static void deleteHero(ArrayList<Technician> list) {
        System.out.println("输入你要删除的英雄编号");
        Scanner sc = new Scanner(System.in);
        String delid = sc.next();
        for (int i = 0; i < list.size(); i++) {
            Technician tec = list.get(i);
            if (tec.getId().equals(delid)){
                list.remove(i);
                System.out.println("删除成功！");
                break;
            }
        }
    }

    private static void replaceHero(ArrayList<Technician> list) {
        System.out.println("输入你要修改的英雄编号");
        Scanner sc = new Scanner(System.in);
        String repid = sc.next();
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Technician tec = list.get(i);
            if (tec.getId().equals(repid)){
                index = i;
                break;
            }
        }
        if (index != -1){
            System.out.println("请输入新的英雄名：");
            String name = sc.next();
            System.out.println("请输入新的年龄：");
            int age = sc.nextInt();
            System.out.println("请输入新的位置：");
            String address = sc.next();

            Technician tech = new Technician(repid,name,age,address);
            list.set(index,tech);
            System.out.println("英雄替换成功！");
        }else {
            System.out.println("此英雄不存在！");
        }

    }

    private static void addHero(ArrayList<Technician> list) {
        Scanner sc = new Scanner(System.in);
        String id;

        while (true){
            boolean flag = false;
            System.out.println("请输入您要添加的编号：");
            id = sc.next();
            for (int i = 0; i < list.size(); i++) {
                Technician tec = list.get(i);
                if (tec.getId().equals(id)){
                    flag = true;
                    System.out.println("此编号已存在，请重新输入！");
                    break;
                }
            }
            if (flag == false){
                break;
            }
        }
        System.out.println("请输入新的英雄名：");
        String name = sc.next();
        System.out.println("请输入新的年龄：");
        int age = sc.nextInt();
        System.out.println("请输入新的位置：");
        String address = sc.next();
        Technician tech = new Technician(id,name,age,address);
        list.add(tech);
        System.out.println("添加成功！");
    }
}
